package org.platform.processor.task.check.impl;

import com.mc.mic.core.tool.helper.SpringContextHelper;
import org.platform.dao.mapper.FdSysconfigMapper;
import org.platform.mo.bussmo.task.CheckItemResult;
import org.platform.processor.task.check.ICheckItem;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;

/**
 * 该类主要用于: ，天线转动预报和实际误差值是否超过阈值
 * 3.	检查天线的方位角/俯仰角实际和预报值是否超限
 *
 * @author FengJie
 * @date 2018/6/25
 */
public class AntennaAngleCheckItemImpl implements ICheckItem {
    /**
     * 方位角实测值 所在列数
     * <p>
     * -1 是因为数组的角标从0开始_
     */
    public static final int POSITION_ACTUAL_COLUMN = 6 - 1;
    /**
     * 方位角预报值 所在列数
     */
    public static final int POSITION_EXPECTED_COLUMN = 11 - 1;
    /**
     * 俯仰角实测值 所在列数
     */
    public static final int PITCH_ANGLE_ACTUAL_COLUMN = 7 - 1;
    /**
     * 俯仰角预报值 所在列数
     */
    public static final int PITCH_ANGLE_EXPECTED_COLUMN = 12 - 1;
    /**
     * 方位角阈值对应的数据表id
     */
    public static final String POSITION_VALUE_KEY = "positionValue";
    /**
     * 俯仰角阈值对应的数据表 id
     */
    public static final String PITCH_ANGLE_VALUE_KEY = "pitchAngleValue";

    @Autowired
    private FdSysconfigMapper sysConfigMapper;

    public AntennaAngleCheckItemImpl() {
        this.sysConfigMapper = SpringContextHelper.getBean(FdSysconfigMapper.class);
    }

    @Override
    public CheckItemResult getCheckResult(String taskSerialNumber, String jobTaskID, File file) throws IOException {
        CheckItemResult result = new CheckItemResult(true, "正常");

        // (方位角/俯仰角)的 阈值/总项数/异常数
        double positionValue = Double.valueOf(sysConfigMapper.selectByPrimaryKey(POSITION_VALUE_KEY).getCurrentvalue());
        double pitchAngleValue = Double.valueOf(sysConfigMapper.selectByPrimaryKey(PITCH_ANGLE_VALUE_KEY).getCurrentvalue());
        double count = 0;
        double positionErrorCount = 0;
        double pitchAngleErrorCount = 0;


        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);
        //第一行为标题
        reader.readLine();

        String line;
        while ((line = reader.readLine()) != null) {
            ++count;
            String[] split = line.split("\t");

            //判断方位角是否超限
            if (Double.parseDouble(split[POSITION_ACTUAL_COLUMN]) < Double.parseDouble(split[POSITION_EXPECTED_COLUMN])) {
                positionErrorCount++;
            }
            //判断俯仰角角是否超限
            if (Double.parseDouble(split[PITCH_ANGLE_ACTUAL_COLUMN]) < Double.parseDouble(split[PITCH_ANGLE_EXPECTED_COLUMN])) {
                pitchAngleErrorCount++;
            }
        }
        reader.close();
        inputStreamReader.close();
        fileInputStream.close();

        //判断是否超过预设的阈值
        if (positionErrorCount / count > positionValue) {
            result.setStatus(false);
            result.setConclusion("天线方位角异常");
        }
        if (pitchAngleErrorCount / count > pitchAngleValue) {
            result.setStatus(false);
            result.setConclusion(result.getConclusion() + "; 天线俯仰角异常");
        }
        return result;
    }
}
